package com.example.drop.vm

import com.example.common.view.vm.BaseViewModel
import com.example.drop.api.DropApiService
import com.example.drop.entity.PointAddParam
import com.example.drop.entity.PointEntityItem
import com.example.drop.entity.TaskEntityItem
import com.example.drop.model.DropRepository
import com.example.drop.model.DropResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class PointViewModel @Inject constructor(private val apiService: DropApiService): BaseViewModel<DropRepository, DropResultData<List<PointEntityItem>>>() {
    fun getPoint(){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.getPoint()
                val res= DropResultData(
                    state = result.state,
                    msg = result.msg,
                    data = result.data
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getPointById(id:Int){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.getPointById(id)
                val res= DropResultData(
                    state = result.state,
                    msg = result.msg,
                    data = listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun postPoint(pointAddParam: PointAddParam){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.postPoint(pointAddParam)
                val res= DropResultData(
                    state = result.state,
                    msg = result.msg,
                    data = listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}